package ejb;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;


import model.Mylike;
import model.Myuser;
import model.Target;

/**
 * Session Bean implementation class LikeFacadeEJB
 */
@Stateless


public class LikeFacadeEJB implements LikeFacadeEJBRemote{
	
	@PersistenceContext
	EntityManager em;

	
	@Override
	public boolean createLike(Myuser myuser, Target targetBean) {
		
		Mylike l = new Mylike();
		
		l.setMyuser(myuser);
		l.setTargetBean(targetBean);
		
		try{
			em.persist(l);
		}catch(Exception e){
			e.printStackTrace();
		}
		return true;
	}
	
	@Override
	public boolean ifLikeExist(Myuser myuser, Target target){
		
		String sql="select l from Mylike l where l.myuser=:u and l.targetBean=:t";
    	Query query= em.createQuery(sql);
    	query.setParameter("u", myuser);
    	query.setParameter("t",target);
    	if(query.getResultList().isEmpty()){
    		return false;
    	}
    	else return true;
		
	}
	
	@Override
	public boolean ifLikeExist2(String username, int targetid){
		
		String sql="select l from Mylike l where l.myuser.username=:u and l.targetBean.targetid=:t";
    	Query query= em.createQuery(sql);
    	query.setParameter("u", username);
    	query.setParameter("t",targetid);
    	if(query.getResultList().isEmpty()){
    		return false;
    	}
    	else return true;
	}


	
    public LikeFacadeEJB() {
        // TODO Auto-generated constructor stub
    }

}
